package com.artPreter.controller.mobile;

import com.artPreter.pojo.*;
import com.artPreter.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.artPreter.utils.PageUtils.getPageList;

@Controller
@RequestMapping("/m/event")
public class MEventContorller {

    @Autowired
    private IArticleService articleService;
    @Autowired
    private IArtistService artistService;
    @Autowired
    private IArtworkService artwrokService;
    @Autowired
    private IDepartService departService;
    @Autowired
    private IArtTypeService artTypeService;
    @Autowired
    private IEventExhibitionContentSectionService eventExhibitionContentSectionService;
    @Autowired
    private IEventExhibitionService eventExhibitionService;
    @Autowired
    private IEventExhibitionSectionTempService eventExhibitionSectionTempService;
    @Autowired
    private IPictureService pictureService;
    @Autowired
    private IEventExhibitionTempService eventExhibitionTempService;
    @Autowired
    private IArtworkService artWorkService;

    @GetMapping("")
    public String event(Integer id, Model model) {

        String eventId = "";


        EventExhibition eventExhibition = eventExhibitionService.getEventExhibitionByInfo(id);

        if (eventExhibition != null) {
            eventId = eventExhibition.getEventId();
        } else {
            return "redirect:/";
        }


        EventExhibitionTemp eventExhibitionTemp = new EventExhibitionTemp();
        eventExhibitionTemp.setEventId(eventId);
        EventExhibitionTemp eventExhibitionTempItem = eventExhibitionSectionTempService.getEventExhibitionTemp(eventExhibitionTemp);
        if (eventExhibitionTempItem == null) {
            return "redirect:/";
        }
        System.out.println(eventExhibition);
        EventExhibitionSectionTemp eventExhibitionSectionTemp = new EventExhibitionSectionTemp();
        eventExhibitionSectionTemp.setEventId(eventId);
        List<EventExhibitionSectionTemp> eventExhibitionSectionTemps = eventExhibitionSectionTempService.getList(eventExhibitionSectionTemp);
        if (eventExhibitionSectionTemps.size() > 0) {
            for (int i = 0; i < eventExhibitionSectionTemps.size(); i++) {
                if (eventExhibitionSectionTemps.get(i).getIsDefault().equals(1)) {
                    return "redirect:/m/event/page?id=" + id + "&sectionId=" + eventExhibitionSectionTemps.get(i).getId();

                }
            }
        } else {
            return "redirect:/";
        }
        // eventExhibitionTemp/getEventExhibitionTemp


        return "redirect:/";
    }

    @GetMapping("/page")
    public String eventPage(Integer id, Integer sectionId, Integer currentPage, Integer type, String name, Model model) {

        if (currentPage == null) {
            currentPage = 1;
        }
        model.addAttribute("currentPage", currentPage);

        EventExhibition eventExhibition = eventExhibitionService.getEventExhibitionByInfo(id);
        String eventId = "";
        if (eventExhibition == null) {
            return "redirect:/";
        } else {
            eventId = eventExhibition.getEventId();
            model.addAttribute("eventExhibition", eventExhibition);
        }
        Depart depart = departService.getDepartById(eventExhibition.getDepartId());
        if (depart == null || depart.getId() == null) {
            return "redirect:/";
        } else {
            model.addAttribute("departInfo", depart);
        }
        EventExhibitionSectionTemp eventExhibitionSectionTemp = new EventExhibitionSectionTemp();
        eventExhibitionSectionTemp.setEventId(eventId);
        model.addAttribute("type", type);
        model.addAttribute("eventId", id);
        model.addAttribute("sectionId", sectionId);
        List<EventExhibitionSectionTemp> eventExhibitionSectionTemps = eventExhibitionSectionTempService.getList(eventExhibitionSectionTemp);
        String tempPath = "";
        List<ArtType> artTypes = new ArrayList<>();

        for (int i = 0; i < eventExhibitionSectionTemps.size(); i++) {
            if (eventExhibitionSectionTemps.get(i).getId().equals(sectionId)) {
                tempPath = eventExhibitionSectionTemps.get(i).getTempAddr();
                EventExhibitionContentSection eventExhibitionContentSection = new EventExhibitionContentSection();
                eventExhibitionContentSection.setSectionId(eventExhibitionSectionTemps.get(i).getSectionId());
                eventExhibitionContentSection.setEventId(eventId);
                List<EventExhibitionContentSection> eventExhibitionContentSectionServiceList = eventExhibitionContentSectionService.getList(eventExhibitionContentSection);
                // 文章 5
                List<Article> articles = new ArrayList<>();
                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片
                List<Picture> pictures = new ArrayList<>();

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                System.out.println(eventExhibitionContentSectionServiceList.size() + "---------");
                for (int j = 0; j < eventExhibitionContentSectionServiceList.size(); j++) {
                    System.out.println(eventExhibitionContentSectionServiceList.get(j).getContentType() + "aaaaaa");
                    // 单页 1
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(1)) {
                        System.out.println(eventExhibitionContentSectionServiceList.get(j).getContent() + "单页内容");
                        pages.add(eventExhibitionContentSectionServiceList.get(j).getContent());
                    }
                    // 艺术家 2
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(2)) {
                        Integer size = 64;
                        // 显示艺术类型
                        artTypes = artTypeService.getList();
                        // 艺术类型搜索
                        if (type != null) {
                            for (int k = 0; k < artTypes.size(); k++) {
                                if (Objects.equals(artTypes.get(k).getId(), type)) {
                                    eventExhibitionContentSectionServiceList.get(j).setSearchType(artTypes.get(k).getTypeId());
                                }
                            }
                        }
                        List<ArtType> artTypeList = new ArrayList<>();
                        artTypeList = artistService.getEventRelatedArtistTypeList(eventExhibitionContentSectionServiceList.get(j));
                        for (int y = 0; y < artTypes.size(); y++) {
                            for (int z = 0; z < artTypeList.size(); z++) {
                                if(artTypeList.get(z).getTypeId().contains(artTypes.get(y).getTypeId())){
                                    artTypes.get(y).setIsShow(1);
                                }
                            }
                        }
                        // 艺术家名字模糊搜索
                        if (name != null && !name.equals("")) {
                            eventExhibitionContentSectionServiceList.get(j).setSearchName(name);
                        }
                        IPage<Artist> artistIPage = artistService.getEventExhibitionRelatedArtistPageList(eventExhibitionContentSectionServiceList.get(j), currentPage, size);
                        // 数据
                        model.addAttribute("artists", artistIPage.getRecords());
                        // 擅长艺术种类
                        model.addAttribute("type", type);
                        // 艺术家名字模糊搜索
                        model.addAttribute("name", name);
                        // 当前页
                        model.addAttribute("currentPage", artistIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages", artistIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages", getPageList((int) artistIPage.getPages(), currentPage, 5));

                        model.addAttribute("artTypes", artTypes);
                    }
                    // 艺术品 3
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(3)) {
                        // 显示艺术类型
                        artTypes = artTypeService.getList();
                        model.addAttribute("artTypes", artTypes);

                        Artwork searchItem = new Artwork();
                        searchItem.setEventId(eventId);
                        searchItem.setName(name);
                        if (type != null) {
                            for (int k = 0; k < artTypes.size(); k++) {
                                if (Objects.equals(artTypes.get(k).getId(), type)) {
                                    searchItem.setArtwoktType(artTypes.get(k).getTypeId());
                                }
                            }
                        }
                        searchItem.setContentId(eventExhibitionContentSectionServiceList.get(j).getContentId());
                        if (type != null) {
                            for (int k = 0; k < artTypes.size(); k++) {
                                if (Objects.equals(artTypes.get(k).getId(), type)) {
                                    searchItem.setArtwoktType(artTypes.get(k).getTypeId());
                                }
                            }

                        }
                        List<Artwork> artworkList = artwrokService.getRelatedArtworkArtTypegetList(searchItem);
                        System.out.println(artworkList.size() + "-------------------艺术种类");
                        for (int k = 0; k < artworkList.size(); k++) {
                            for (int z = 0; z < artTypes.size(); z++) {
                                if (artworkList.get(k).getArtwoktType().equals(artTypes.get(z).getTypeId())) {
                                    artTypes.get(z).setStatus(3);
                                }
                            }
                        }
                        IPage<Artwork> artworkIPage = artwrokService.getEventExhibitionRelatedArtworkPageList(searchItem, currentPage);
                        // 名字模糊搜索
                        model.addAttribute("name", name);
                        // 数据
                        model.addAttribute("artworks", artworkIPage.getRecords());
                        // 当前页
                        model.addAttribute("currentPage", artworkIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages", artworkIPage.getPages());
                        // 分页页码
                        // 分页页码
                        Integer jump = 3;
                        if(currentPage==1||currentPage==2){
                            jump = 2;
                        }else if(currentPage>2){
                            jump = 1;
                        }
                        model.addAttribute("pages", getPageList((int) artworkIPage.getPages(), currentPage, jump));
                        if (tempPath.equals("/zpzx")) {
                            tempPath = "pc/eventExhibition/eventExhibition_artwork_online";
                        }
                    }
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(4)) {
                        pictures = pictureService.getEventExhibitionRelatedPictureList(eventExhibitionContentSectionServiceList.get(j), -99);
                        model.addAttribute("pictures", pictures);
                    }
                    // 文章 5
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(5)) {
                        IPage<Article> articleIPage = articleService.getEventExhibitionRelatedArticlePageList(eventExhibitionContentSectionServiceList.get(j), currentPage);

                        //articles = articleService.getEventExhibitionRelateArticleList(eventExhibitionContentSectionServiceList.get(j),2);
                        // 数据
                        model.addAttribute("articles", articleIPage.getRecords());
                        // 当前页
                        model.addAttribute("currentPage", articleIPage.getCurrent());
                        // 总页数
                        model.addAttribute("totalPages", articleIPage.getPages());
                        // 分页页码
                        model.addAttribute("pages", getPageList((int) articleIPage.getPages(), currentPage, 5));
                    }
                    // 超连接 6
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(6)) {
                        System.out.println("超连接");
                        if (eventExhibitionContentSectionServiceList.get(j).getContent() != null) {
                            return "redirect:" + eventExhibitionContentSectionServiceList.get(j).getContent();
                        }
                    }
                }
                EventExhibitionTemp eventExhibitionTempItem = new EventExhibitionTemp();
                eventExhibitionTempItem.setEventId(eventId);
                EventExhibitionTemp eventExhibitionTemp = eventExhibitionTempService.getEventExhibitionTemp(eventExhibitionTempItem);
                if (eventExhibitionTemp != null) {
                    model.addAttribute("eventExhibitionTemp", eventExhibitionTemp);
                } else {
                    return "redirect:/";
                }


                // 模板单页数据
                model.addAttribute("pageDetails", pages);


            }
        }
        model.addAttribute("eventExhibitionSectionTemps", eventExhibitionSectionTemps);
        switch (tempPath) {
            case "pc/event/simple_artist_list":
                tempPath = "mobile/event/artist_list";
                break;
            case "pc/event/simple_event_page":
                tempPath = "mobile/event/page";
                break;
            case "pc/event/simple_artwork_list":
                tempPath = "mobile/event/artwork_list";
                break;
            case "pc/event/simple_artwork_online_list":
                tempPath = "mobile/event/online_list";
                break;
            case "pc/event/simple_article_list":
                tempPath = "mobile/event/article_list";
                break;
            case "pc/event/simple_picture_list":
                tempPath = "mobile/event/picture_list";
                break;

        }


        if (tempPath.equals("")) {
            return "redirect:/";
        } else {
            System.out.println(tempPath + "模板");
            return tempPath;
        }
    }

    /**
     * @param id
     * @param sectionId
     * @param aid
     * @param model
     * @return
     */
    @GetMapping("/article/detail")
    public String eventExhibitionArticleDetail(Integer id, Integer sectionId, Integer aid, Model model) {
        // 头部菜单
        List<EventExhibitionContentSection> menuList = eventExhibitionContentSectionService.getMenuList();
        model.addAttribute("menus", menuList);

        Article article = articleService.getArticleByNumberId(aid);
        if (article != null) {

            model.addAttribute("article", article);
        }
        EventExhibition eventExhibition = eventExhibitionService.getEventExhibitionByInfo(id);
        String eventId = "";
        if (eventExhibition == null) {
            System.out.println("没有该机构");
            return "redirect:/";
        } else {
            eventId = eventExhibition.getEventId();
            model.addAttribute("eventExhibition", eventExhibition);
        }

        Depart depart = departService.getDepartById(eventExhibition.getDepartId());
        if (depart == null || depart.getId() == null) {
            return "redirect:/";
        } else {
            model.addAttribute("departInfo", depart);
        }


        EventExhibitionSectionTemp eventExhibitionSectionTemp = new EventExhibitionSectionTemp();
        eventExhibitionSectionTemp.setEventId(eventId);
        model.addAttribute("eventExhibitionId", id);
        model.addAttribute("sectionId", sectionId);
        List<EventExhibitionSectionTemp> eventExhibitionSectionTemps = eventExhibitionSectionTempService.getList(eventExhibitionSectionTemp);
        System.out.println("模板" + eventExhibitionSectionTemps.size() + "条");
        String tempPath = "";

        for (int i = 0; i < eventExhibitionSectionTemps.size(); i++) {
            if (eventExhibitionSectionTemps.get(i).getId().equals(sectionId)) {
                model.addAttribute("sectionName", eventExhibitionSectionTemps.get(i).getName());
                tempPath = eventExhibitionSectionTemps.get(i).getTempAddr();
                EventExhibitionContentSection eventExhibitionContentSection = new EventExhibitionContentSection();
                eventExhibitionContentSection.setSectionId(eventExhibitionSectionTemps.get(i).getSectionId());
                eventExhibitionContentSection.setEventId(eventId);
                List<EventExhibitionContentSection> eventExhibitionContentSectionServiceList = eventExhibitionContentSectionService.getList(eventExhibitionContentSection);


                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                System.out.println(eventExhibitionContentSectionServiceList.size() + "---------");
                for (int j = 0; j < eventExhibitionContentSectionServiceList.size(); j++) {
                    System.out.println(eventExhibitionContentSectionServiceList.get(j).getContentType() + "aaaaaa");
                    // 单页 1
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(1)) {
                        System.out.println(eventExhibitionContentSectionServiceList.get(j).getContent() + "单页内容");
                        pages.add(eventExhibitionContentSectionServiceList.get(j).getContent());
                    }
                    // 艺术家 2
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(2)) {
                        artists = artistService.getEventExhibitionRelatedArtistList(eventExhibitionContentSectionServiceList.get(j));
                        System.out.println("艺术家" + artists.size() + "条");
                        model.addAttribute("artists", artists);
                    }
                    // 艺术品 3
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(3)) {
                        // 小于10代表单列表模板
                        Integer count = 0;
                        if (eventExhibitionSectionTemps.get(i).getContentType() < 10) {
                            count = -99;
                        } else {
                            count = 6;
                        }
                        // 作品在线
                        if (tempPath.equals("/zpzx")) {

                            // 数据
                            tempPath = "pc/eventExhibition/eventExhibition_artwork_online";
                        } else {
                            artworks = artwrokService.getEventExhibitionRelatedArtworkList(eventExhibitionContentSectionServiceList.get(j), count);
                            System.out.println("艺术品" + artworks.size() + "条");
                            model.addAttribute("artworks", artworks);
                        }
                    }
                    // 文章 5
                    if (eventExhibitionContentSectionServiceList.get(j).getContentType().equals(5)) {
                    }
                }
                EventExhibitionTemp eventExhibitionTempItem = new EventExhibitionTemp();
                eventExhibitionTempItem.setEventId(eventId);
                EventExhibitionTemp eventExhibitionTemp = eventExhibitionTempService.getEventExhibitionTemp(eventExhibitionTempItem);
                if (eventExhibitionTemp != null) {
                    System.out.println(eventExhibitionTemp.getBannerUrl());
                    model.addAttribute("eventExhibitionTemp", eventExhibitionTemp);
                } else {
                    return "redirect:/";
                }


                // 模板单页数据
                model.addAttribute("pageDetails", pages);


            }
        }
        tempPath = "mobile/event/article_detail";
        model.addAttribute("eventExhibitionSectionTemps", eventExhibitionSectionTemps);

        // eventExhibitionTemp/getEventExhibitionTemp


        if (tempPath.equals("")) {
            return "redirect:/";
        } else {
            return tempPath;
        }
    }

    @GetMapping("/artwork/detail")
    public String eventArtworkDetail(Integer id,Integer sectionId,Integer wid, Model model) {


        // 头部菜单
        List<EventExhibitionContentSection> menuList = eventExhibitionContentSectionService.getMenuList();
        model.addAttribute("menus",menuList);
        List<ArtType> artTypes = new ArrayList<>();
        Artwork artwork = artWorkService.getArtWorkById(wid);
        if(artwork != null){
            artTypes = artTypeService.getList();
            for (int i = 0; i < artTypes.size(); i++) {
                if(artTypes.get(i).getTypeId().equals(artwork.getArtwoktType())){
                    artwork.setArtTypeName(artTypes.get(i).getName());
                }
            }
            model.addAttribute("artwork",artwork);
        }



        EventExhibition eventExhibition = eventExhibitionService.getEventExhibitionByInfo(id);
        String eventId = "";
        if(eventExhibition == null){
            System.out.println("没有该机构");
            return "redirect:/";
        }else{
            eventId = eventExhibition.getEventId();
            model.addAttribute("eventExhibition",eventExhibition);
        }
        Depart depart = departService.getDepartById(eventExhibition.getDepartId());
        if(depart==null||depart.getId()==null){
            return "redirect:/";
        }else{
            model.addAttribute("departInfo",depart);
        }
        EventExhibitionSectionTemp eventExhibitionSectionTemp = new EventExhibitionSectionTemp();
        eventExhibitionSectionTemp.setEventId(eventId);
        model.addAttribute("eventId",id);
        model.addAttribute("sectionId",sectionId);
        List<EventExhibitionSectionTemp> eventExhibitionSectionTemps = eventExhibitionSectionTempService.getList(eventExhibitionSectionTemp);
        System.out.println("模板"+eventExhibitionSectionTemps.size()+"条");
        String tempPath = "";

        for (int i = 0; i < eventExhibitionSectionTemps.size(); i++) {
            if(eventExhibitionSectionTemps.get(i).getId().equals(sectionId)){
                model.addAttribute("sectionName",eventExhibitionSectionTemps.get(i).getName());
                model.addAttribute("id",eventExhibitionSectionTemps.get(i).getId());
                tempPath = eventExhibitionSectionTemps.get(i).getTempAddr();
                EventExhibitionContentSection eventExhibitionContentSection = new EventExhibitionContentSection();
                eventExhibitionContentSection.setSectionId(eventExhibitionSectionTemps.get(i).getSectionId());
                eventExhibitionContentSection.setEventId(eventId);
                List<EventExhibitionContentSection> eventExhibitionContentSectionServiceList = eventExhibitionContentSectionService.getList(eventExhibitionContentSection);



                // 单页 1
                List<String> pages = new ArrayList<>();
                // 艺术品 3
                List<Artwork> artworks = new ArrayList<>();
                // 图片

                // 艺术家 2
                List<Artist> artists = new ArrayList<>();
                System.out.println(eventExhibitionContentSectionServiceList.size()+"---------");
                for (int j = 0; j < eventExhibitionContentSectionServiceList.size(); j++) {
                    System.out.println(eventExhibitionContentSectionServiceList.get(j).getContentType()+"aaaaaa");
                    // 单页 1
                    if(eventExhibitionContentSectionServiceList.get(j).getContentType().equals(1)){
                        System.out.println(eventExhibitionContentSectionServiceList.get(j).getContent()+"单页内容");
                        pages.add(eventExhibitionContentSectionServiceList.get(j).getContent());
                    }
                    // 艺术家 2
                    if(eventExhibitionContentSectionServiceList.get(j).getContentType().equals(2)){
                        artists = artistService.getEventExhibitionRelatedArtistList(eventExhibitionContentSectionServiceList.get(j));
                        System.out.println("艺术家"+artists.size()+"条");
                        model.addAttribute("artists",artists);
                    }
                    // 艺术品 3
                    if(eventExhibitionContentSectionServiceList.get(j).getContentType().equals(3)){
                        // 小于10代表单列表模板
                        Integer count = 0;
                        if(eventExhibitionSectionTemps.get(i).getContentType()<10){
                            count = -99;
                        }else{
                            count = 6;
                        }
                        // 作品在线
                        if(tempPath.equals("/zpzx")){

                            // 数据
                            tempPath = "pc/eventExhibition/eventExhibition_artwork_online";
                        }else{
                            artworks = artwrokService.getEventExhibitionRelatedArtworkList(eventExhibitionContentSectionServiceList.get(j),count);
                            System.out.println("艺术品"+artworks.size()+"条");
                            model.addAttribute("artworks",artworks);
                        }
                    }
                    // 文章 5
                    if(eventExhibitionContentSectionServiceList.get(j).getContentType().equals(5)){
                    }
                }
                EventExhibitionTemp eventExhibitionTempItem = new EventExhibitionTemp();
                eventExhibitionTempItem.setEventId(eventId);
                EventExhibitionTemp eventExhibitionTemp = eventExhibitionTempService.getEventExhibitionTemp(eventExhibitionTempItem);
                if(eventExhibitionTemp!= null){
                    System.out.println("banner"+eventExhibitionTemp);
                    model.addAttribute("eventExhibitionTemp",eventExhibitionTemp);
                }else{
                    return "redirect:/";
                }



                // 模板单页数据
                model.addAttribute("pageDetails",pages);


            }
        }
        tempPath = "mobile/event/artwork_detail";
        model.addAttribute("eventExhibitionSectionTemps",eventExhibitionSectionTemps);

        // eventExhibitionTemp/getEventExhibitionTemp


        if(tempPath.equals("")){
            return "redirect:/";
        }else{
            return tempPath;
        }
    }


}
